%% Smoothing code
% Code smooths the results obtained

%% Figure 8 and 9 (f_entrant plots)

list = {'beta.txt', 'gamma.txt', 'pi.txt', 'H.txt'}; 
ppath = {os_dir(setts.dir,'data\debt\'),os_dir(setts.dir,'data\nodebt\')};
deg=3;
idx=7;
    
for i=1:length(list)

	eval(['ppath2=os_dir(setts.dir,''data\figure_8_and_9_',list{i},''')',';'])
	f_ent = readtable(ppath2);
	var_list = {'vec', 'change_entrant', 'share_entrant', 'change_mass_entrant', 'change_products_entrant', 'change_products_incumbent', 'change_mass_incumbent', 'change_incumbent', ...
		'diff_f_incumbent', 'diff_f_entrant', 'diff_m_incumbent', 'diff_m_entrant', 'diff_products_incumbent', 'diff_products_entrant'};
	for m=2:14
		eval(['f_ent.',var_list{m},'_s_c=polyval(polyfix(table2array(f_ent(:,1)),table2array(f_ent(:,',num2str(m),')),deg,table2array(f_ent(idx,1)),table2array(f_ent(idx,',num2str(m),'))),table2array(f_ent(:,1)));'])
	end
	writetable(f_ent,ppath2,'Delimiter',' ');
end

%% Figure 10 (elasticity figure)

var_list = {'vec', 'change_entrant', 'change_incumbent', 'change_mass_entrant', 'change_mass_incumbent', 'change_products_entrant', 'change_products_incumbent' };
f_ent = readtable(os_dir(setts.dir,'data\figure_10.txt'));
deg=3;
idx=20;

for m=2:7
    eval(['f_ent.',var_list{m},'_s_c=polyval(polyfix(table2array(f_ent(:,1)),table2array(f_ent(:,',num2str(m),')),deg,table2array(f_ent(idx,1)),table2array(f_ent(idx,',num2str(m),'))),table2array(f_ent(:,1)));'])
end
writetable(f_ent,os_dir(setts.dir,'data\figure_10.txt'),'Delimiter',' ');
